package com.app.controller;


import com.app.pojo.Item;
import com.app.pojo.Order;
import com.app.pojo.Result;
import com.app.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@CrossOrigin
public class OrderController {
    @Autowired
    OrderService orderService;

    @PostMapping ("/order")
    public Result add(@RequestBody Order order){
        log.info("创建订单");
        orderService.add(order);
        return Result.success();
    }

    @DeleteMapping("/order/{order_id}")
    public Result delete(@PathVariable int order_id){
        log.info("删除订单");
        orderService.delete(order_id);
        return Result.success();
    }

    @PutMapping("/order")
    public Result update(@RequestBody Order order){
        log.info("修改订单");
        orderService.update(order);
        return Result.success();
    }

    @GetMapping("/order/{user_id}")
    public Result List(@PathVariable int user_id){
        log.info("根据用户ID：{}，查询订单",user_id);
        List<Order> orderList = orderService.list(user_id);
        if(orderList.isEmpty()){
            return Result.error("该用户无订单信息");
        }else{
            return Result.success(orderList);
        }
    }

    @GetMapping("/order/status/{order_id}")
    public Result select(@PathVariable int order_id){
        log.info("根据订单ID:{},查询订单状态",order_id);
        int status = orderService.select(order_id);
        if(status != 1 && status != 2 && status != 3){
            return Result.error("该订单不存在");
        }else{
            return Result.success(status);
        }
    }

    @GetMapping("/order/detail/{order_id}")
    public Result List2(@PathVariable int order_id){
        log.info("根据订单ID：{}，查询订单详情",order_id);
        List<Order> itemList = orderService.list2(order_id);
        if(itemList.isEmpty()){
            return Result.error("该订单不存在");
        }else{
            return Result.success(itemList);
        }
    }

    //查询所有订单
    @GetMapping("/order/selectAll")
    public Result selectAll(){
        List<Order> orderList = orderService.selectAll();
        return Result.success(orderList);
    }
}
